<template>
  <div class="container">
    <div class="center-container">
      <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px"
               class="demo-userForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userForm.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="userForm.gender">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <!--中国行政区划代码：https://www.mca.gov.cn/n156/n186/c110746/content.html-->
        <el-form-item label="籍贯" prop="nativePlace">
          <el-select v-model="userForm.nativePlace" placeholder="请选择籍贯">
            <el-option value="110000" label="北京市"></el-option>
            <el-option value="120000" label="天津市"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理员" prop="admin">
          <el-switch v-model="userForm.admin"></el-switch>
        </el-form-item>
        <el-form-item label="爱好" prop="hobby">
          <el-checkbox-group v-model="userForm.hobby">
            <el-checkbox label="骑行" name="hobby"></el-checkbox>
            <el-checkbox label="跑步" name="hobby"></el-checkbox>
            <el-checkbox label="网游" name="hobby"></el-checkbox>
            <el-checkbox label="旅行" name="hobby"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item label="个性签名" prop="desc">
          <el-input type="textarea" v-model="userForm.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('userForm')">保 存</el-button>
          <el-button @click="resetForm('userForm')">重 置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      @close="closeDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      center>
      <span><pre>{{ userForm }}</pre></span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "validate-01",
  data() {
    return {
      userForm: {
        name: '',
        nativePlace: '',
        admin: false,
        hobby: [],
        gender: '',
        desc: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: ['blur', 'change']},
          {min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        nativePlace: [
          {required: true, message: '请选择籍贯', trigger: 'blur'}
        ],
        hobby: [
          {type: 'array', required: true, message: '请至少选择一个爱好', trigger: 'change'},
          {type: 'array', min: 2, message: '请至少选择2个爱好', trigger: 'change'},
        ],
        gender: [
          {required: true, message: '请选择籍贯', trigger: 'change'}
        ],
        desc: [
          {required: true, message: '请填写个性签名', trigger: 'blur'},
          {max: 200, message: '个性签名长度不能大于200', trigger: 'blur'},
        ]
      },

      centerDialogVisible: false,
    };
  },
  methods: {
    closeDialog() {
      this.centerDialogVisible = false;
      this.$refs.userForm.resetFields();
    },
    submitForm(formName) {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.centerDialogVisible = true;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      //this.$refs[formName].resetFields();
      this.$refs.userForm.resetFields();
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 设置父容器的高度为视口高度，使其充满整个屏幕 */
}

.center-container {
  max-width: 600px; /* 设置内部容器的最大宽度，根据需求进行调整 */
}
</style>
